package com.huizu.project.apiController;

import com.huizu.common.annotation.Anonymous;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.enums.BusinessType;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.domain.ProZy;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.IProZyService;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专业管理Controller
 * 
 * @author zhangmx
 * @date 2024-05-11
 */
@Anonymous
@RestController
@RequestMapping("/api/project/prozy")
public class ApiProZyController extends BaseController
{
    @Autowired
    private IProZyService proZyService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询专业管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:prozy:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProZy proZy) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> zyIds = auths.stream().filter(auth -> auth.getType().equals("zy")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proZy.setZyIds(zyIds);
        }
        startPage();
        List<ProZy> list = proZyService.selectProZyList(proZy);
        return getDataTable(list);
    }

    /**
     * 查询专业管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:prozy:list')")
    @PostMapping("/listTotal")
    public TableDataInfo listTotal(HttpServletRequest request, ProZy proZy) {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);
        if (!SecurityUtils.isAdmin(Long.parseLong(userId))) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(Long.parseLong(userId));
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> zyIds = auths.stream().filter(auth -> auth.getType().equals("zy")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proZy.setZyIds(zyIds);
        }
        List<ProZy> list = proZyService.selectProZyList(proZy);
        return getDataTable(list);
    }

    /**
     * 导出专业管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:prozy:export')")
    @Log(title = "专业管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProZy proZy) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> zyIds = auths.stream().filter(auth -> auth.getType().equals("zy")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proZy.setZyIds(zyIds);
        }
        List<ProZy> list = proZyService.selectProZyList(proZy);
        ExcelUtil<ProZy> util = new ExcelUtil<ProZy>(ProZy.class);
        util.exportExcel(response, list, "专业管理数据");
    }

    /**
     * 获取专业管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:prozy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(proZyService.selectProZyById(id));
    }

    /**
     * 新增专业管理
     */
    @PreAuthorize("@ss.hasPermi('project:prozy:add')")
    @Log(title = "专业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProZy proZy)
    {
        return toAjax(proZyService.insertProZy(proZy));
    }

    /**
     * 修改专业管理
     */
    @PreAuthorize("@ss.hasPermi('project:prozy:edit')")
    @Log(title = "专业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProZy proZy)
    {
        return toAjax(proZyService.updateProZy(proZy));
    }

    /**
     * 删除专业管理
     */
    @PreAuthorize("@ss.hasPermi('project:prozy:remove')")
    @Log(title = "专业管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proZyService.deleteProZyByIds(ids));
    }
}
